package com.ychs.lawyerback01.service.imp;

import com.ychs.lawyerback01.dto.SealQueryDTO;
import com.ychs.lawyerback01.mapper.SealRecordMapper;
import com.ychs.lawyerback01.pojo.SealRecord;
import com.ychs.lawyerback01.service.SealRecordService;
import com.ychs.lawyerback01.util.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SealRecordServiceImpl implements SealRecordService {

    @Autowired
    private SealRecordMapper sealRecordMapper;

    @Override
    public SealRecord addSealRecord(SealRecord sealRecord, String username) {
        Date now = new Date();
        sealRecord.setCreateTime(now);
        sealRecord.setUpdateTime(now);
        sealRecord.setCreatedBy(username);
        sealRecord.setUpdatedBy(username);

        sealRecordMapper.insert(sealRecord);
        return sealRecord;
    }

    @Override
    public SealRecord updateSealRecord(Long id, SealRecord sealRecord, String username) {
        // 检查记录是否存在
        SealRecord existingRecord = sealRecordMapper.selectById(id);
        if (existingRecord == null) {
            throw new RuntimeException("印章记录不存在");
        }

        // 设置需要更新的字段
        existingRecord.setDate(sealRecord.getDate());
        existingRecord.setReason(sealRecord.getReason());
        existingRecord.setCopies(sealRecord.getCopies());
        existingRecord.setSealType(sealRecord.getSealType());
        existingRecord.setHandler(sealRecord.getHandler());
        existingRecord.setRemarks(sealRecord.getRemarks());
        existingRecord.setUpdateTime(new Date());
        existingRecord.setUpdatedBy(username);

        sealRecordMapper.update(existingRecord);
        return existingRecord;
    }

    @Override
    public void deleteSealRecord(Long id) {
        SealRecord sealRecord = sealRecordMapper.selectById(id);
        if (sealRecord == null) {
            throw new RuntimeException("印章记录不存在");
        }
        sealRecordMapper.deleteById(id);
    }

    @Override
    public SealRecord getSealRecordById(Long id) {
        return sealRecordMapper.selectById(id);
    }

    @Override
    public Map<String, Object> getSealRecordsByPage(SealQueryDTO queryDTO) {
        // 准备查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", queryDTO.getStartDate());
        params.put("endDate", queryDTO.getEndDate());
        params.put("sealType", queryDTO.getSealType());
        params.put("handler", queryDTO.getHandler());
        params.put("offset", queryDTO.getStart());
        params.put("pageSize", queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10);

        // 查询数据
        List<SealRecord> records = sealRecordMapper.selectByCondition(params);
        long total = sealRecordMapper.countByCondition(params);
        int pageNum = queryDTO.getPage() != null && queryDTO.getPage() > 0 ? queryDTO.getPage() : 1;
        int pageSize = queryDTO.getPageSize() != null && queryDTO.getPageSize() > 0 ? queryDTO.getPageSize() : 10;

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", records);
        result.put("total", total);
        result.put("page", pageNum);
        result.put("pageSize", pageSize);
        result.put("totalPages", (int) Math.ceil((double) total / pageSize));

        return result;
    }

    @Override
    public void exportExcel(SealQueryDTO queryDTO, HttpServletResponse response) throws IOException {
        // 查询所有符合条件的数据
        queryDTO.setPage(1);
        queryDTO.setPageSize(Integer.MAX_VALUE);

        Map<String, Object> params = new HashMap<>();
        params.put("startDate", queryDTO.getStartDate());
        params.put("endDate", queryDTO.getEndDate());
        params.put("sealType", queryDTO.getSealType());
        params.put("handler", queryDTO.getHandler());
        params.put("offset", 0);
        params.put("pageSize", Integer.MAX_VALUE);

        List<SealRecord> records = sealRecordMapper.selectByCondition(params);

        // 导出Excel
        ExcelUtils.exportSealRecords(records,response);
    }
}
